import random

from django.shortcuts import render, HttpResponse
from django.conf import settings
from django import forms
from web import models
from web.forms.bootstrap import BootStrapForm
from utils.tencent.sms import send_sms_single
from utils import encrypt
from django_redis import get_redis_connection
from django.conf import settings
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError


class WikiModelForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.Wiki
        exclude = ['project', 'depth']

    def __init__(self, request, wiki_id, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 找到想要的字段把他绑定显示的数据重置
        # 数据 = 去数据库中获取，当前项目所有的wiki标题
        total_data_list = [("", "请选择"), ]  # 提供空项选择
        # data_list = models.Wiki.objects.filter(project=request.tracer.project).values_list('id', 'title')
        data_list = models.Wiki.objects.exclude(id=wiki_id).exclude(parent_id=wiki_id).exclude(depth=3). \
            filter(project=request.tracer.project).values_list('id', 'title')

        total_data_list.extend(data_list)  # 将空选项添加到data_list里面去
        # self.fields['parent'].choices = data_list
        self.fields['parent'].choices = total_data_list


class WikiModelForm1(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.Wiki
        exclude = ['project', 'depth']

    def __init__(self, request, wiki_id, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 找到想要的字段把他绑定显示的数据重置
        # 数据 = 去数据库中获取，当前项目所有的wiki标题
        total_data_list = [("", "请选择"), ]  # 提供空项选择
        # data_list = models.Wiki.objects.filter(project=request.tracer.project).values_list('id', 'title')
        # data_list = models.Wiki.objects.exclude(id=wiki_id).exclude(parent_id=wiki_id).exclude(depth=3). \
        #     filter(project=request.tracer.project).values_list('id', 'title')
        data_list = models.Wiki.objects.exclude(depth=3).filter(project=request.tracer.project).values_list('id',
                                                                                                            'title')
        total_data_list.extend(data_list)  # 将空选项添加到data_list里面去
        # self.fields['parent'].choices = data_list
        self.fields['parent'].choices = total_data_list
